﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Oroboros.Save_Data;
using System.Reflection;

namespace Save_Editor.Windows
{
    /// <summary>
    /// Interaction logic for winNewItem.xaml
    /// </summary>
    public partial class winNewItem : Window
    {
        public ItemSaveData Item
        {
            get
            {
                return (ItemSaveData)this.DataContext;
            }
            set
            {
                this.DataContext = value;
            }
        }

        public winNewItem()
        {
            InitializeComponent();

            tabSupply.Margin = tabGun.Margin = tabAmmo.Margin = new Thickness(6, 6, 6, 6);

            FieldInfo[] fInfos = typeof(ItemDB).GetFields();
            foreach (FieldInfo f in fInfos) cmbItems.Items.Add(new State((string[])f.GetValue(null)));

            fInfos = typeof(AmmoDB).GetFields();
            foreach (FieldInfo f in fInfos) ammobox.Items.Add(f.GetValue(null));
        }

        private void cmbItems_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            tabAmmo.Visibility = System.Windows.Visibility.Hidden;
            tabGun.Visibility = System.Windows.Visibility.Hidden;
            tabSupply.Visibility = System.Windows.Visibility.Hidden;
            if (cmbItems.SelectedIndex == -1) return;
            State s = cmbItems.SelectedItem as State;
            ItemSaveData newItem = new ItemSaveData();
            switch (s.ItemType)
            {
                case State.Item_Type.Ammo:
                    {
                        tabAmmo.Visibility = System.Windows.Visibility.Visible;
                        tabGun.Visibility = System.Windows.Visibility.Hidden;
                        tabSupply.Visibility = System.Windows.Visibility.Hidden;
                        newItem = new AmmoBoxItemSaveData();
                        newItem.ItemClassName = s.ClassName;
                        break;
                    }
                case State.Item_Type.Gun:
                    {
                        tabAmmo.Visibility = System.Windows.Visibility.Hidden;
                        tabGun.Visibility = System.Windows.Visibility.Visible;
                        tabSupply.Visibility = System.Windows.Visibility.Hidden;
                        GunItemSaveData gun = new GunItemSaveData();
                        gun.ItemClassName = s.ClassName;
                        gun.LoadedAmmoClassName = s.AmmoClassName;
                        newItem = gun;
                        break;
                    }
                case State.Item_Type.Melee:
                    {
                        tabAmmo.Visibility = System.Windows.Visibility.Hidden;
                        tabGun.Visibility = System.Windows.Visibility.Hidden;
                        tabSupply.Visibility = System.Windows.Visibility.Hidden;
                        newItem = new MeleeItemSaveData();
                        newItem.ItemClassName = s.ClassName;
                        break;
                    }
                case State.Item_Type.Misc:
                    {
                        tabAmmo.Visibility = System.Windows.Visibility.Hidden;
                        tabGun.Visibility = System.Windows.Visibility.Hidden;
                        tabSupply.Visibility = System.Windows.Visibility.Hidden;
                        newItem.ItemClassName = s.ClassName;
                        break;
                    }
                case State.Item_Type.Supply:
                    {
                        tabAmmo.Visibility = System.Windows.Visibility.Hidden;
                        tabGun.Visibility = System.Windows.Visibility.Hidden;
                        tabSupply.Visibility = System.Windows.Visibility.Visible;
                        newItem.ItemClassName = s.ClassName;
                        break;
                    }
            }
            Item = newItem;
        }

        private void clickOK(object sender, RoutedEventArgs e)
        {
            this.DialogResult = new bool?(true);
        }

        private void clickCancel(object sender, RoutedEventArgs e)
        {
            this.DialogResult = new bool?(false);
        }
    }

    public class State
    {
        public string[] data;
        public State(string[] data)
        {
            this.data = data;
        }

        public string ClassName
        {
            get
            {
                return data[0];
            }
        }

        public string AmmoClassName
        {
            get
            {
                return data[1];
            }
        }

        public enum Item_Type{
            None,
            Supply,
            Ammo,
            Gun,
            Melee,
            Misc
        }

        public Item_Type ItemType
        {
            get
            {
                if (ClassName.Contains("weap_") && string.IsNullOrEmpty(AmmoClassName))
                    return Item_Type.Melee;
                if (ClassName.Contains("weap_") && !string.IsNullOrEmpty(AmmoClassName))
                    return Item_Type.Gun;
                if (ClassName.Contains("supply_") || ClassName.Contains("food_") || ClassName.Contains("Med_"))
                    return Item_Type.Supply;
                if (ClassName.Contains("ammobox_"))
                    return Item_Type.Ammo;
                return Item_Type.Misc;
            }
        }

        public override string ToString()
        {
            return ClassName;
        }
    }
}
